import requests
from lxml import etree
import parsel

for page in range(1, 11):
    print(f'————————正在抓取{page}页——————————')
    url = f'https://tuchong.com/rest/tags/%E7%BE%8E%E5%A5%B3/posts?page={page}&count=20&order=weekly'

    head = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36x-requested-with: XMLHttpRequest'
    }

    resp = requests.get(url, headers=head)
    josn_datd = resp.json()

    # print(josn_datd)

    # 提取相册的url地址

    pic_url_list = josn_datd['postList']
    for pic in pic_url_list:
        phoyo_url = pic['url']
        # print(phoyo_url)


        # 发送每个相册的请求，在解析
        html_data = requests.get(phoyo_url, headers=head).text

        # 找到相册集中每张图片的url连接
        selenctor = parsel.Selector(html_data)   # 转换数据类型

        img_url_list = selenctor.xpath('//article[@class="post-content"]/img/@src').getall()
        # print(img_url_list)

        for img_url in img_url_list:
            img_data = requests.get(img_url, headers=head).content

            # 保存图片数据
            img_name = img_url.split('/')[-1]

            with open('img2\\' + img_name, mode='wb') as f:
                f.write(img_data)
                print(f'{img_name}爬取成功！！')










